package com.sam.thread.demo4;

/**
 * 输入线程
 *
 * 对资源对象resource对象中成员变量赋值
 *
 * 第一次赋值 zhangsan nan
 * 第二次赋值 lisi nv
 * 。。。
 *
 */
public class Input implements Runnable {

    private Resource resource;

    public Input(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        int i = 0;
        while(true){
            synchronized (resource){
                if(resource.flag == true){
                    try {
                        resource.wait();//等待
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else{
                    if(i%2 == 0){
                        resource.sex = "男";
                        resource.name = "张三";
                    }else{
                        resource.sex = "女";
                        resource.name = "李四";
                    }
                    i++;
                    //唤醒对方线程   将flag改为false
                    resource.flag = true;
                    resource.notify();
                }
            }


        }
    }
}
